package com.daily.DailyStudy.接口.实现API接口统一JSON格式返回.controller;

import com.daily.DailyStudy.接口.实现API接口统一JSON格式返回.entity.Result;
import com.daily.DailyStudy.接口.实现API接口统一JSON格式返回.core.ResponseResultBody;
import com.daily.DailyStudy.接口.实现API接口统一JSON格式返回.exception.ResultException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
 * 后端返回给前端一般情况下使用JSON格式, 定义如下
 * {
 *     "code": 200,
 *     "message": "OK",
 *     "data": {
 *
 *     }
 * }
 *
 * */
@RestController
@RequestMapping("/commonRespose")
@ResponseResultBody
public class HelloController {

    private static final HashMap<String, Object> INFO;

    static {
        INFO = new HashMap<>();
        INFO.put("name", "galaxy");
        INFO.put("age", "70");
    }

    @GetMapping("/hello")
    public Map<String, Object> hello() {
        return INFO;
    }

    /** 测试重复包裹 */
    @GetMapping("result")
    public Result<Map<String, Object>> helloResult() {
        return Result.success(INFO);
    }

    @GetMapping("helloError")
    public HashMap<String, Object> helloError() throws Exception {
        throw new Exception("helloError");
    }

    @GetMapping("helloMyError")
    public HashMap<String, Object> helloMyError() throws Exception {
        throw new ResultException();
    }

}
